<template>
  <div>
    <h2>用户管理</h2>
    <el-row>
      <el-col :span="12">
        <!-- 添加用户表单 -->
        <el-card class="user-card">
          <div slot="header" class="clearfix">
            <span class="user-name">添加用户</span>
          </div>
          <el-form :model="newUser" ref="newUserForm" :rules="newUserRules" label-width="100px">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="newUser.username"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="newUser.email"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="newUser.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="addUser">添加用户</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
      <el-col :span="12">
        <!-- 用户列表 -->
        <el-card class="user-card">
          <div slot="header" class="clearfix">
            <span class="user-name">用户列表</span>
          </div>
          <el-table :data="users" style="width: 100%">
            <el-table-column prop="username" label="用户名"></el-table-column>
            <el-table-column prop="email" label="邮箱"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" @click="editUser(scope.row)">编辑</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="deleteUser(scope.$index)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <!-- 编辑用户对话框 -->
    <el-dialog title="编辑用户" :visible.sync="editDialogVisible">
      <el-form :model="editedUser" ref="editUserForm" :rules="editedUserRules" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editedUser.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editedUser.email"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="editedUser.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveEditedUser">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newUser: {
        username: '',
        email: '',
        password: ''
      },
      newUserRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
        ]
      },
      editDialogVisible: false,
      editedUser: {
        id: '',
        username: '',
        email: '',
        password: ''
      },
      editedUserRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
        ]
      },
      users: [
        { id: 1, username: 'User 1', email: 'user1@example.com', password: 'password1' },
        { id: 2, username: 'User 2', email: 'user2@example.com', password: 'password2' },
        // Add more users here
      ]
    };
  },
  methods: {
    addUser() {
      this.$refs.newUserForm.validate(valid => {
        if (valid) {
          const newUser = { ...this.newUser, id: Date.now() };
          this.users.push(newUser);
          this.clearNewUser();
          this.$message.success('用户添加成功');
        }
      });
    },
    editUser(user) {
      this.editedUser = { ...user };
      this.editDialogVisible = true;
    },
    saveEditedUser() {
      this.$refs.editUserForm.validate(valid => {
        if (valid) {
          const index = this.users.findIndex(user => user.id === this.editedUser.id);
          if (index !== -1) {
            this.users.splice(index, 1, this.editedUser);
            this.editDialogVisible = false;
            this.$message.success('用户信息已更新');
          }
        }
      });
    },
    deleteUser(index) {
      this.$confirm('确认删除该用户吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.users.splice(index, 1);
        this.$message.success('用户删除成功');
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    clearNewUser() {
      this.newUser = {
        username: '',
        email: '',
        password: ''
      };
    }
  }
};
</script>

<style scoped>
.user-card {
  margin-bottom: 20px;
}

.user-name {
  font-size: 18px;
  line-height: 40px;
}

.dialog-footer {
  text-align: right;
}
</style>
